<?php
////////////////////////////////////////////////////////////////////////////////////////
// Class: DbConnector
// Purpose: Connect to a database, MySQL version
////////////////////////////////////////////////////////////////////////////////////////
require_once 'SystemComponent.class.php';

class DbConnector extends SystemComponent {

    var $link;
    var $result;
    var $query;

    //*** Function: DbConnector, Purpose: Connect to the database ***
    function DbConnector() {

        // Load settings from parent class
        $settings = SystemComponent::getSettings();

        // Get the main settings from the array we just loaded
        $host = $settings['dbhost'];
        $db = $settings['dbname'];
        $user = $settings['dbusername'];
        $pass = $settings['dbpassword'];

        // Connect to the database
        $this->link = mysql_connect($host, $user, $pass);
        mysql_select_db($db);
        register_shutdown_function(array(&$this, 'close'));
    }

    //*** Function: query using custom query parameter, Purpose: Execute a database query, Note: Overwrites old query variable  ***
    function query($query) {

        $this->query = $query;
        $this->result = mysql_query($query, $this->link);
        return  $this->result;

    }

    //*** Function: query using the objects query parameter, Purpose: Execute a database query ***
    function query() {

        return mysql_query($this->query, $this->link);

    }

    //*** Function: fetchArray using custom result parameter, Purpose: Get array of query results, Note: Overwrites old result variable ***
    function fetchArray($result) {

        $this->result = mysql_fetch_array($result);
        return $this->result;

    }

    //*** Function: fetchArray using the objects result parameter, Purpose: Get array of query results ***
    function fetchArray() {

        return mysql_fetch_array($this->result);

    }

    //*** Function: fetchArrayFirst using custom result parameter, Purpose: Returns the first result in the resultset, Note: Overwrites old result variable ***
    function fetcArrayFirst($result) {
        
        $this->result = mysql_fetch_array($result);
        
        return $this->result[0];
        
    }

    //*** Function: fetchArrayFirst using the objects result parameter, Purpose: Returns the first result in the resultset ***
    function fetchArrayFirst() {

        return $this->result[0];

    }

    //*** Function: close, Purpose: Close the connection ***
    function close() {

        mysql_close($this->link);
        
    }
}

?>